package com.yiwei.ydd.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.ydd.R;
import com.yiwei.ydd.adapter.MyOilCardAdapter;
import com.yiwei.ydd.api.Api;
import com.yiwei.ydd.api.bean.OilcardDefaultBean;
import com.yiwei.ydd.api.bean.OilcardDeleteBean;
import com.yiwei.ydd.api.model.OilcardListModel;
import com.yiwei.ydd.base.BaseActivity;
import com.yiwei.ydd.util.Dialog;
import com.yiwei.ydd.util.Network;
import com.yiwei.ydd.util.RxLifeUtil;
import com.yiwei.ydd.util.ToastUtil;
import com.yiwei.ydd.util.Util;
import com.yiwei.ydd.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class MyOilCardActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.btn_agency)
    TextView btnAgency;
    @BindView(R.id.btn_add)
    TextView btnAdd;

    private Unbinder unbiner;
    private MyOilCardAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_oil_card);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的加油卡");
        txtRight.setVisibility(View.VISIBLE);
        txtRight.setText("代办记录");
        adapter = new MyOilCardAdapter(this);
        adapter.setOnChooseListener(new MyOilCardAdapter.OnChooseListener() {
            @Override
            public void onEdit(OilcardListModel.DatasBean item) {
                Intent intent = new Intent(MyOilCardActivity.this, OilCardAddActivity.class);
                intent.putExtra("is_edit", true);
                intent.putExtra("oil_data", item);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
            }

            @Override
            public void onDelete(String id) {
                Dialog.showDefaultDialog(MyOilCardActivity.this, "", "您确定删除该油卡吗？", "取消", "确定", false, new Dialog.DialogDefaultClickListener() {
                    @Override
                    public void confirm() {
                        getAccountDelete(id);
                    }

                    @Override
                    public void cancel() {

                    }
                });
            }

            @Override
            public void onDefault(String id) {
                getOilDefault(id);
            }
        });
        list.setAdapter(adapter);


    }

    @Override
    protected void onResume() {
        super.onResume();
        getAccountList();
    }

    public void getAccountList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        loadingDialog.show();
        Api.api_service.getOilcardList().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.datas != null && result.datas.size() != 0) {
                        layoutEmpty.setVisibility(View.GONE);
                    } else {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    }
                    adapter.update(result.datas);
                });
    }

    public void getOilDefault(String oilcard_id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }


        loadingDialog.show();
        OilcardDefaultBean bean = new OilcardDefaultBean();
        bean.oilcard_id = oilcard_id;
        Api.api_service.getOilcardDefault(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "设置成功");
                    getAccountList();
                });
    }

    public void getAccountDelete(String id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        OilcardDeleteBean item = new OilcardDeleteBean();
        item.id = id;

        loadingDialog.show();
        Api.api_service.getOilcardDelete(item).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "删除成功");
                    getAccountList();
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_agency, R.id.btn_add, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                ToastUtil.makeText(this, "敬请期待");
//                Util.startActivity(this, AgencyRecordActivity.class);
                break;
            case R.id.btn_agency:
                ToastUtil.makeText(this, "敬请期待");
//                Util.startActivity(this, OilAgencyActivity.class);
                break;
            case R.id.btn_add:
                Intent intent=new Intent(this,OilCardAddActivity.class);
                intent.putExtra("is_go_home",true);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }
}
